package in.muton.edushare.components.messaging.recievepanel;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.messaging.util.MessageTypeParser;
import in.muton.edushare.panels.images.ImageWithLink;
import in.muton.edushare.processing.objects.ObjectTypeId;
import in.muton.edushare.schedules.widgets.DateLabel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.dao.GroupsDAO;
import com.edushare.common.dao.MessagesDAO;
import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Users;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class UserMessagePanel extends Panel {
	public UserMessagePanel(String id, final IModel<Messages> model) {
		super(id, model);
		WebMarkupContainer wmc = new WebMarkupContainer("background");
		add(wmc);

		add
		(
			new ImageWithLink(
					"userLink", 
					new CayenneModel<IFileUploadable>(getSenderEntity(	model.getObject().getReceiverId(), 
																		model.getObject().getReceiverType())),
					ApplicationConstants.FILE_SUFFIX_ICON)
		);

		wmc.add(MessageTypeParser.getURLPanel(
				model.getObject().getReceiverId(), model.getObject()
						.getReceiverType(), "name"));
		wmc.add(new EduShareURLPanel("sender", new CayenneModel<Users>(model
				.getObject().getUser())));
		add(new DateLabel("date", model.getObject().getCreationTimestamp(), 0));
		wmc.add(new Label("msg", model.getObject().getMessage())
				.setEscapeModelStrings(false));

		add(new Link<Void>("delete") {
			private static final long serialVersionUID = -7258411621026470557L;

			@Override
			public void onClick() {
				new MessagesDAO(EduShareSession.get().getSessionORMInterface())
						.deleteMsg(model.getObject());
			}

			@Override
			public boolean isVisible() {
				Users user = EduShareSession.get().getLoggedInUser();

				if (model.getObject().getReceiverType() == Messages.RECEIVER_TYPE_USER
						&& model.getObject().getAllMessageRecipients()
								.contains(user))
					return true;

				CayenneList<Users> owners = ObjectTypeId.getOwner(model
						.getObject().getReceiverId(), model.getObject()
						.getReceiverType());

				if (owners.contains(user)) {
					return true;
				}
				return false;
			}
		});
	}

	private IFileUploadable getSenderEntity(Integer id, Integer type) {
		ORMInterface orm = EduShareSession.get().getSessionORMInterface();
		switch (type) {
		case Messages.RECEIVER_TYPE_UNIVERSITY:
			return new UniversitiesDAO(orm).getUnivById(id);
		case Messages.RECEIVER_TYPE_COURSE:
			return new CourseInstancesDAO(orm).getCourseInstanceById(id);
		case Messages.RECEIVER_TYPE_DEPARTMENT:
			return new DepartmentsDAO(orm).getDepartmentByPk(id);
		case Messages.RECEIVER_TYPE_USER:
			return new UsersDAO(orm).getUserByPk(id);
		case Messages.RECEIVER_TYPE_GROUP:
			return new GroupsDAO(orm).getGroupById(id);
		}
		return null;
	}

	private static final long serialVersionUID = 773802807479772075L;
}
